﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TakbisBilgiSorgu_v1.wsUygulamaYetkiServis;
using TakbisBilgiSorgu_v1.wsUygulamaTakbisServis;
using System.Configuration;
using System.Text;

namespace TakbisBilgiSorgu_v1
{
    public partial class SFUlkeIslemleri : System.Web.UI.Page
    {
        UygulamaYetkiServis uygulamaYetkiServis = new UygulamaYetkiServis();
        UygulamaTakbisServis uygulamaTakbisServis = new UygulamaTakbisServis();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (((Kullanici)Session["kullanici"]) == null)
                    {
                        MesajVer(ConfigurationManager.AppSettings["GirisYapmalisinizMesaji"].ToString());
                        Response.Redirect("Login.aspx");
                    }
                    else
                    {
                        if (!uygulamaYetkiServis.GrupYetkiVarMiKontrolYeni(((Kullanici)Session["kullanici"]).GrupID, "SFUlkeIslemleri.aspx") || ((Kullanici)Session["kullanici"]).AktifMi != 1)
                        {
                            MesajVer(ConfigurationManager.AppSettings["YetkinizYokMesaji"].ToString());
                            Response.Redirect("AnaSayfa.aspx");
                        }
                        else
                        {
                            divGetirUlkeBilgiIDDen.Visible = false;
                            divgvUlke.Visible = false;
                            divgvTasinmaz.Visible = false;
                            Session["TasinmazTapuBolumDurum"] = TapuBolumDurum.Aktif;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                    if (ex.ToString().Contains("Sunucu isteği işleyemedi."))
                        MesajVer(ConfigurationManager.AppSettings["YetkiServisHatasi"].ToString());
                    else
                        MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
                }
            }
        }

        protected void btnTumUlkeleriGetir_Click(object sender, EventArgs e)
        {
            try
            {
                UlkeBilgi[] ulkeArray = uygulamaTakbisServis.GetirUlkeBilgiTum(((Kullanici)Session["kullanici"]).ID);
                if (ulkeArray != null)
                {
                    Session["ulkeList"] = ulkeArray.ToList();
                    divGetirUlkeBilgiIDDen.Visible = false;
                    divgvUlke.Visible = false;
                    divgvTasinmaz.Visible = false;
                    ViewState["ulkeList"] = Session["ulkeList"];
                    gvUlke.DataSource = Session["ulkeList"];
                    gvUlke.DataBind();
                    divgvUlke.Visible = true;
                    divUlkeSorguKriter.Visible = false;
                }
                else
                {
                    Session["ulkeList"] = null;
                    MesajVer("Ülkeler Getirilemedi.");
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                    if (ex.ToString().Contains("Sunucu isteği işleyemedi."))
                        MesajVer(ConfigurationManager.AppSettings["TakbisServisHatasi"].ToString());
                    else
                        MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
                }
            }
        }

        protected void btnIDdenUlkeGetir_Click(object sender, EventArgs e)
        {
            try
            {
                //divGetirUlkeBilgiIDDen
                divGetirUlkeBilgiIDDen.Visible = true;
                divgvUlke.Visible = false;
                divgvTasinmaz.Visible = false;
                divUlkeSorguKriter.Visible = false;
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void btnGetirUlkeBilgiIDDen_Click(object sender, EventArgs e)
        {
            try
            {
                if (uygulamaYetkiServis.BosDegerMi(txtUlkeID.Text.Trim()) && uygulamaYetkiServis.SqlInjectionControl(txtUlkeID.Text.Trim()))
                {
                    Session["ulkeList"] = new List<UlkeBilgi>();
                    ((List<UlkeBilgi>)Session["ulkeList"]).Add(uygulamaTakbisServis.GetirUlkeBilgiIDDen(Convert.ToInt16(txtUlkeID.Text.Trim()), ((Kullanici)Session["kullanici"]).ID));
                    if (Session["ulkeList"] != null && ((List<UlkeBilgi>)Session["ulkeList"]).Count > 0)
                    {
                        ViewState["ulkeList"] = Session["ulkeList"];
                        gvUlke.DataSource = (List<UlkeBilgi>)Session["ulkeList"];
                        gvUlke.DataBind();
                        divgvUlke.Visible = true;
                    }
                    else
                        MesajVer("Ülkeler Getirilemedi.");
                }
                else
                    MesajVer("Sorgulama Kriterlerini Kontrol Edip Lütfen Tekrar Deneyiniz.");
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                    if (ex.ToString().Contains("Sunucu isteği işleyemedi."))
                        MesajVer(ConfigurationManager.AppSettings["TakbisServisHatasi"].ToString());
                    else
                        MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
                }
            }
        }

        protected void btnTasinmazGetir_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["SecilenUlkeBilgi"] != null)
                {
                    if (Session["TasinmazTapuBolumDurum"] != null)
                    {
                        Session["secilenUlkeTasinmazlari"] = new List<Tasinmaz>();
                        Session["secilenUlkeTasinmazlari"] = uygulamaTakbisServis.GetirMalikTasinmazTasinmazTipten(MalikTip.Ulke, TasinmazTip.Hepsi, ((UlkeBilgi)Session["SecilenUlkeBilgi"]).UlkeBilgiID, (TapuBolumDurum)Session["TasinmazTapuBolumDurum"], ((Kullanici)Session["kullanici"]).ID).ToList();
                        if (Session["secilenUlkeTasinmazlari"] == null || ((List<Tasinmaz>)Session["secilenUlkeTasinmazlari"]).Count == 0)
                            MesajVer("Seçilen Ülkeye Bağlı Tasınmaz Bulunmamaktadır.");
                        else
                        {
                            ViewState["secilenUlkeTasinmazlari"] = (List<Tasinmaz>)Session["secilenUlkeTasinmazlari"];
                            gvTasinmaz.DataSource = (List<Tasinmaz>)Session["secilenUlkeTasinmazlari"];
                            gvTasinmaz.DataBind();
                            divgvTasinmaz.Visible = true;
                        }
                    }
                    else
                        MesajVer("Aktif, Pasif Veya Hepsi Seçeneklerinden Bir Tanesini Seçip Lütfen Tekrar Deneyiniz.");
                }
                else
                    MesajVer("Ülke Seçip Lütfen Tekrar Deneyiniz.");
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                    if (ex.ToString().Contains("Sunucu isteği işleyemedi."))
                        MesajVer(ConfigurationManager.AppSettings["TakbisServisHatasi"].ToString());
                    else
                        MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
                }
            }
        }

        protected void btnUlkeExceleYaz_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvUlke.Rows.Count != 0)
                {
                    byte[] data = uygulamaTakbisServis.UlkeExceleYaz(((List<UlkeBilgi>)Session["ulkeList"]).ToArray());
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    Response.AddHeader("content-disposition", "attachment;filename=Rapor.xls");
                    Response.AddHeader("Content-Type", "application/Excel");
                    Response.ContentEncoding = Encoding.GetEncoding("Windows-1252");
                    Response.ContentType = "application/vnd.xls";
                    Response.AddHeader("Content-Length", data.Length.ToString());
                    Response.BinaryWrite(data);
                    Response.Flush();
                    Response.End();
                }
                else
                    MesajVer(ConfigurationManager.AppSettings["YazdirilacakKayitYok"].ToString());
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                    if (ex.ToString().Contains("Sunucu isteği işleyemedi."))
                        MesajVer(ConfigurationManager.AppSettings["TakbisServisHatasi"].ToString());
                    else
                        MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
                }
            }
        }

        protected void btnTasinmazExceleYaz_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvTasinmaz.Rows.Count != 0)
                {
                    byte[] data = uygulamaTakbisServis.TasinmazExceleYaz(((List<Tasinmaz>)Session["secilenUlkeTasinmazlari"]).ToArray());
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    Response.AddHeader("content-disposition", "attachment;filename=Rapor.xls");
                    Response.AddHeader("Content-Type", "application/Excel");
                    Response.ContentEncoding = Encoding.GetEncoding("Windows-1252");
                    Response.ContentType = "application/vnd.xls";
                    Response.AddHeader("Content-Length", data.Length.ToString());
                    Response.BinaryWrite(data);
                    Response.Flush();
                    Response.End();
                }
                else
                    MesajVer(ConfigurationManager.AppSettings["YazdirilacakKayitYok"].ToString());
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                    if (ex.ToString().Contains("Sunucu isteği işleyemedi."))
                        MesajVer(ConfigurationManager.AppSettings["TakbisServisHatasi"].ToString());
                    else
                        MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
                }
            }
        }

        protected void gvUlke_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                if (ViewState["ulkeList"] != null)
                {
                    gvUlke.PageIndex = e.NewPageIndex;
                    gvUlke.DataSource = ViewState["ulkeList"];
                }
                else
                    gvUlke.DataSource = null;
                gvUlke.DataBind();
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void gvTasinmaz_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                if (ViewState["secilenUlkeTasinmazlari"] != null)
                {
                    gvTasinmaz.PageIndex = e.NewPageIndex;
                    gvTasinmaz.DataSource = ViewState["secilenUlkeTasinmazlari"];
                }
                else
                    gvTasinmaz.DataSource = null;
                gvTasinmaz.DataBind();
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void gvUlke_RowCreated(object sender, GridViewRowEventArgs e)
        {
            SatirIsaretle(e);
        }

        protected void gvTasinmaz_RowCreated(object sender, GridViewRowEventArgs e)
        {
            SatirIsaretle(e);
        }

        protected void gvUlke_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (gvUlke.Rows.Count > 1)
                {
                    foreach (GridViewRow row in gvUlke.Rows)
                    {
                        if (row.RowIndex == gvUlke.SelectedIndex)
                            row.ToolTip = string.Empty;
                        else
                        {
                            Session["SecilenUlkeID"] = null;
                            Session["SecilenUlkeID"] = Convert.ToDecimal(gvUlke.DataKeys[gvUlke.SelectedIndex].Values[0]);
                            Session["SecilenUlkeBilgi"] = ((List<UlkeBilgi>)Session["ulkeList"]).Where(o => o.UlkeBilgiID == ((decimal)Session["SecilenUlkeID"])).ToList()[0];
                        }
                    }
                }
                else
                {
                    Session["SecilenUlkeID"] = null;
                    Session["SecilenUlkeID"] = Convert.ToDecimal(gvUlke.DataKeys[gvUlke.SelectedIndex].Values[0]);
                    Session["SecilenUlkeBilgi"] = ((List<UlkeBilgi>)Session["ulkeList"]).Where(o => o.UlkeBilgiID == ((decimal)Session["SecilenUlkeID"])).ToList()[0];
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void gvUlke_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvUlke, "Select$" + e.Row.RowIndex);
                    e.Row.ToolTip = "Click to select this row.";
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void gvTasinmaz_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "TapuBolumDurum")) == "Pasif")
                    e.Row.ForeColor = System.Drawing.Color.Red;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvTasinmaz, "Select$" + e.Row.RowIndex);
                    e.Row.ToolTip = "Click to select this row.";
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void SatirIsaretle(GridViewRowEventArgs e)
        {
            try
            {
                //mouse gridin üzerine geldiğinde satırlar işaretlensin diye yaptım
                if (e.Row.RowType != DataControlRowType.DataRow)
                    return;
                // eger yaratilan satir farkliysa data satirindan buradan cik 
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#F0FFFF'");
                // mouse overina bu stil attribute'ini ekle 
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
                // mouse out ise eski stile don
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void rbTasinmazAktif_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbTasinmazAktif.Checked == true)
                {
                    rbTasinmazPasif.Checked = false;
                    rbTasinmazHepsi.Checked = false;
                    Session["TasinmazTapuBolumDurum"] = TapuBolumDurum.Aktif;
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void rbTasinmazPasif_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbTasinmazPasif.Checked == true)
                {
                    rbTasinmazAktif.Checked = false;
                    rbTasinmazHepsi.Checked = false;
                    Session["TasinmazTapuBolumDurum"] = TapuBolumDurum.Pasif;
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void rbTasinmazHepsi_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbTasinmazHepsi.Checked == true)
                {
                    rbTasinmazAktif.Checked = false;
                    rbTasinmazPasif.Checked = false;
                    Session["TasinmazTapuBolumDurum"] = TapuBolumDurum.Hepsi;
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void MesajVer(string mesaj)
        {
            ClientScript.RegisterStartupScript(this.GetType(), " Uyarı.. ", "ShowPopup('" + mesaj + "');", true);
        }

        protected void SorguTiplerineGit()
        {
            try
            {
                divGetirUlkeBilgiIDDen.Visible = false;
                divgvUlke.Visible = false;
                divgvTasinmaz.Visible = false;
                divUlkeSorguKriter.Visible = true;
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
                MesajVer(ConfigurationManager.AppSettings["HataMesaji"].ToString());
            }
        }

        protected void lnkSorguTiplerineGit_Click(object sender, EventArgs e)
        {
            SorguTiplerineGit();
        }

        protected void lnkSorguTiplerineGit1_Click(object sender, EventArgs e)
        {
            SorguTiplerineGit();
        }
    }
}
